package com.isoftstone.common.utils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.*;

/**
 * @描述：xml转换
 * @author：guoX
 * @date：2017/4/27
 */
public class XmlUtil {

    /**
     * 对象转xml (企业)
     *
     * @param c      对象class
     * @param object 对象
     * @return
     */
    public static String ObjectToXml(Class c, Object object) {
        StringWriter writer = new StringWriter();
        try {
            JAXBContext context = JAXBContext.newInstance(c);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //防止文件中文乱码
            m.marshal(object, writer);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return writer.toString();
    }


    /**
     * @param c   对象class
     * @param xml xml数据
     * @return
     */
    public static Object XmlToObject(Class c, String xml) {
        try {
            JAXBContext context2 = JAXBContext.newInstance(c);
            Unmarshaller unmarshaller = context2.createUnmarshaller();
            // Objects object = unmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes("GBK")));
            Object object = unmarshaller.unmarshal(new StringReader(xml));
            return object;
        } catch (JAXBException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static Object XmlToObject(Class c, File xml) {
        try {
            JAXBContext context2 = JAXBContext.newInstance(c);
            Unmarshaller unmarshaller = context2.createUnmarshaller();
            Object object = unmarshaller.unmarshal(xml);
            return object;
        } catch (JAXBException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String ReaderJson(String filePath) throws IOException {
        StringBuffer sb = new StringBuffer();
        try {
            FileReader fileReader = new FileReader(new File(filePath));
            BufferedReader bufReader = new BufferedReader(fileReader);

            LineNumberReader reader = new LineNumberReader(bufReader);

            String line;

            try {
                while ((line = reader.readLine()) != null) {

                    sb.append(line).append(System.getProperty("line.separator"));

                }
                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            String content = sb.toString();

            System.out.println(content);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        return sb.toString();
    }


    public static void main(String[] args) throws Exception {
        String str = "铜川市印台区失业和农村养老保险管理中心（城乡养老基金收入户）";
        System.out.println(str.getBytes("gbk").length);
    }

}
